package com.lifuixng.picturebackend.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lifuixng.picturebackend.annotation.AuthCheck;
import com.lifuixng.picturebackend.common.BaseResponse;
import com.lifuixng.picturebackend.common.DeleteRequest;
import com.lifuixng.picturebackend.common.ResultUtils;
import com.lifuixng.picturebackend.constant.UserConstant;
import com.lifuixng.picturebackend.model.dto.user.*;
import com.lifuixng.picturebackend.model.entity.User;
import com.lifuixng.picturebackend.model.vo.user.UserLoginVO;
import com.lifuixng.picturebackend.model.vo.user.UserVO;
import com.lifuixng.picturebackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author Mr.LiFuXing
 * @Date 2024/12/14 14:51
 * @Title UserController
 * @Package com.lifuixng.picturebackend.controller
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 用户注册
     * @param userRegisterRequest
     * @return
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(UserRegisterRequest userRegisterRequest) {

        log.info("用户注册{}", userRegisterRequest);
        Long result = userService.userRegister(userRegisterRequest);
        return ResultUtils.success(result);
    }

    /**
     * 用户登录
     * @param userLoginRequest
     * @param request
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<UserLoginVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {

        log.info("用户登录{}", userLoginRequest);
        UserLoginVO userLoginVO = userService.userLogin(userLoginRequest, request);
        return ResultUtils.success(userLoginVO);
    }

    /**
     * 获取登录的用户信息
     * @param request
     * @return
     */
    @GetMapping("/get/login")
    public BaseResponse<UserLoginVO> getLoginUserInfo(HttpServletRequest request) {

        log.info("获取登录的用户信息");
        UserLoginVO userLoginVO = userService.getLoginUser(request);
        return ResultUtils.success(userLoginVO);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @PostMapping("/logout")
    @AuthCheck
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {

        log.info("用户退出登录");
        boolean result = userService.userLogout(request);
        return ResultUtils.success(result);
    }

    /**
     * 添加用户
     * @param userAddRequest
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest){

        log.info("管理员添加用户{}",userAddRequest);
        Long i = (long) userService.addUser(userAddRequest);
        return ResultUtils.success(i);
    }

    /**
     * 根据id删除用户
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> deleteUser(@RequestBody DeleteRequest deleteRequest){

        log.info("管理员删除用户{}",deleteRequest);
        Long i = (long)userService.deleteById(deleteRequest);
        return ResultUtils.success(i);
    }

    /**
     * 修改用户
     * @param userUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> updateUser(@RequestBody UserUpdateRequest userUpdateRequest){

        log.info("管理员更新用户{}",userUpdateRequest);
        Long i = (long)userService.updateUser(userUpdateRequest);
        return ResultUtils.success(i);
    }

    @GetMapping("/get/vo")
    @AuthCheck()
    public BaseResponse<UserVO> getUserVOById(Long id){

        log.info("根据id获取用户信息（脱敏）{}",id);
        UserVO userVO = userService.getUserVOById(id);
        return ResultUtils.success(userVO);
    }

    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<User> getUserById(Long id){

        log.info("根据id获取用户信息{}",id);
        User user = userService.getUserById(id);
        return ResultUtils.success(user);
    }


    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVO>> userVOListByPage(@RequestBody UserQueryRequest userQueryRequest){

        log.info("获取用户列表分页信息{}",userQueryRequest);
        Page<UserVO> userVOPage = userService.userListByPage(userQueryRequest);
        return ResultUtils.success(userVOPage);
    }
}
